package com.xoa.service.party;


import com.xoa.dao.party.OrgDepartmentMapper;
import com.xoa.dao.party.OrgGovimentMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.OrgDepartmentWithBLOBs;
import com.xoa.model.party.OrgGoviment;
import com.xoa.model.users.Users;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 张丽军 on 2018/1/10.
 */
@Service
public class OrgGovimentService {

    @Autowired
    OrgGovimentMapper orgGovimentMapper;

    @Autowired
    OrgDepartmentMapper orgDepartmentMapper;

    public ToJson<OrgGoviment> addOrgGoviment(HttpServletRequest request, OrgGoviment orgGoviment) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        orgGoviment.setSenduser(users.getUserId());
        try{
            int a=orgGovimentMapper.addOrgGoviment(orgGoviment);
            if(a>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("error");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<OrgGoviment> updateOrgGoviment(HttpServletRequest request, OrgGoviment orgGoviment) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        orgGoviment.setSenduser(users.getUserId());
        try{
            int a=orgGovimentMapper.updateOrgGoviment(orgGoviment);
            if(a>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("error");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<OrgGoviment> orgGovimentList(HttpServletRequest request, OrgGoviment orgGoviment, Integer page, Integer pageSize, boolean useFlag) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        try{
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            map.put("orgGoviment",orgGoviment);
            //获取所有的组织信息表
            List<OrgDepartmentWithBLOBs> orgDeplist = orgDepartmentMapper.queryDepIdList(null);
               List<Integer> list1 = new ArrayList<Integer>();
               list1.add(orgGoviment.getOrgdepid());
            List<Integer>reOrg = this.reOrgId(orgDeplist, orgGoviment.getOrgdepid(), list1);

            map.put("reOrg", reOrg);
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("orgGoviment",orgGoviment);
            map2.put("reOrg", reOrg);

            List<OrgGoviment> list=orgGovimentMapper.orgGovimentList(map);
            if(list.size()>0){
                toJson.setObj(list);
                toJson.setTotleNum(orgGovimentMapper.orgGovimentListCount(map2));
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("数据为空");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson<OrgGoviment> orgGovimentDelete(HttpServletRequest request, Integer gid) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        try{
            int a=orgGovimentMapper.orgGovimentDelete(gid);
            if(a>0){
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("error");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<OrgGoviment> orgGovimentDetail(HttpServletRequest request, Integer gid) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        try{
            OrgGoviment orgGoviment=orgGovimentMapper.orgGovimentDetail(gid);
            //添加附件处理
            List<Attachment> attachmentList=new ArrayList<Attachment>();
            if(orgGoviment.getAddname()!=null&&!"".equals(orgGoviment.getAddname())){
                attachmentList= GetAttachmentListUtil.returnAttachment(orgGoviment.getDelname(),orgGoviment.getAddname(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_attachment);
            }
            orgGoviment.setAttachmentList(attachmentList);

            if(orgGoviment!=null){
                toJson.setObject(orgGoviment);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("error");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson<OrgGoviment> orgGovimentUser(HttpServletRequest request, Integer deptId) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        try{
            List<OrgGoviment> list=orgGovimentMapper.orgGovimentUser(deptId);
            if(list.size()>0){
                toJson.setObj(list);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("数据为空");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson<OrgGoviment> getCheckCount(HttpServletRequest request, Integer deptId, String userId) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        try{
            //获取巡查统计数
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("deptId",deptId);
            map.put("userId",userId);
            int orgGoviment=orgGovimentMapper.getCheckCount(map);
            if(orgGoviment>0){
                toJson.setObject(orgGoviment);
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("error");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    public List<Integer> reOrgId(List<OrgDepartmentWithBLOBs> orgDeplist, Integer orgdepId, List<Integer> list ){
            for (OrgDepartmentWithBLOBs orgDepartmentWithBLOBs : orgDeplist){
                if (orgDepartmentWithBLOBs.getOrgDeptParent().equals(orgdepId)){
                    list.add(orgDepartmentWithBLOBs.getOrgDeptId());
                    reOrgId(orgDeplist, orgDepartmentWithBLOBs.getOrgDeptId(), list);
                }
            }
            return list;
    }


    public ToJson<OrgGoviment> orgGovimentList1(HttpServletRequest request, OrgGoviment orgGoviment, Integer page, Integer pageSize, boolean useFlag) {
        ToJson<OrgGoviment> toJson=new ToJson<OrgGoviment>(1,"error");
        try{
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            map.put("orgGoviment",orgGoviment);
            /*//获取所有的组织信息表
            List<OrgDepartmentWithBLOBs> orgDeplist = orgDepartmentMapper.queryDepIdList(null);
            List<Integer> list1 = new ArrayList<Integer>();
            list1.add(orgGoviment.getOrgdepid());
            List<Integer>reOrg = this.reOrgId(orgDeplist, orgGoviment.getOrgdepid(), list1);*/

            /*map.put("reOrg", reOrg);*/
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("orgGoviment",orgGoviment);
           /* map2.put("reOrg", reOrg);*/

            List<OrgGoviment> list=orgGovimentMapper.orgGovimentList(map);
            if(list.size()>0){
                toJson.setObj(list);
                toJson.setTotleNum(orgGovimentMapper.orgGovimentListCount(map2));
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("数据为空");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setFlag(1);
        }
        return toJson;
    }
}
